//引入上边封装的axios文件
import request from "@/components/request";
import {segmentedEncryptionByPlaintext} from "@/utils/jesencrypt.js";

const sysKey = "key";
/**
 * post 请求
 * @param data 参数
 */
export const resetPost = (url, data, key = "key") => {

    let randomStr = getRandom(5);
    let time = getTime();
    let keyValue = time + "_" + randomStr + "_" + sysKey;
    let encryption = segmentedEncryptionByPlaintext(keyValue, 16, "_")
    data[key] = encryption;

    return request({
        url: url,
        method: "POST",
        headers: {
            "content-type": "application/json;charset=UTF-8",
            // response.setHeader("Access-Control-Allow-Origin","*");
        },
        data
    });
};


/**
 * 获取随机字符串 默认5
 * @param idLength 长度 默认 5
 * @returns {string} 字符串
 */
export const getRandom = (idLength = 5) => {
    // let value = Math.random().toString(36).slice(2)
    // console.log(value)
    const characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let randomID = '';
    for (let i = 0; i < idLength; i++) {
        const randomIndex = Math.floor(Math.random() * characters.length);
        randomID += characters.charAt(randomIndex);
    }
    return randomID;
}
getRandom();
export const getTime = () => {
    let nowTime = new Date().getTime();
    return nowTime;
}
